package com.demo.my.Controller;

import com.demo.my.Entity.Attendance;
import com.demo.my.Service.AttendanceService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@WebServlet("/addAttend")
public class AddAttendanceServlet extends HttpServlet {
    public AttendanceService attendanceService=new AttendanceService();

    private int[] checkedAttend(int employeeNumber,String day) throws Exception {
        String sql = "select *from attendance where employee_number=? and day=?";
            Object[] objs = {employeeNumber,day};
            Attendance one = attendanceService.getOne(sql, new Attendance(), objs);
            int[] data=new int[2];
            if (one.getId()==null){
                //可以签到并且只能签到,不能签退0
                return data;
            }else {
                //已经有记录了要判断可不可以签退
                if(one.getEndTime()==null|| one.getEndTime().equals("")){
                    data[0]=1;
                    data[1]=one.getId();
                    return data; //已经签到了,不能再签到,可以签退
                }else{
                    data[0]=2;
                    return data;//不能签到也能不能签退了
                }
            }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String employeeNumber = req.getParameter("employeeNumber");
            Date date = new Date();
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd a HH:mm:ss");
            String format = sdf1.format(date);
            String[] split = format.split(" ");
            String s1 = split[0];//日期
            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
            Date date1 = format1.parse(s1);
            String s2 = split[1];//上下午
            String s3 = split[2];
            String vid = req.getParameter("vid");
            Attendance attendance = new Attendance(null, Integer.parseInt(employeeNumber), date1, s2, null, null, null, null, null, null);
            int[] ints = checkedAttend(Integer.parseInt(employeeNumber), s1);
            int status = ints[0];
            //vid     0 签到      1 签退
            //status  0 可以签到并且只能签到,不能签退  1已经签到了,不能再签到,可以签退   2不能签到也能不能签退了
            if (vid.equals("0")&&status==0) {
                Calendar car1 = Calendar.getInstance();
                car1.setTime(date);
                Calendar car2 = Calendar.getInstance();
                car2.set(car1.get(Calendar.YEAR), car1.get(Calendar.MARCH), car1.get(Calendar.DAY_OF_MONTH), 8, 0, 0);
                int i = car2.compareTo(car1);
                String ss = i >= 0 ? "正常" : "迟到";
                System.out.println("ss = " + ss);
                attendance.setStartTime(s3);
                attendance.setStartType(ss);
                attendanceService.addAttendance(attendance);
                resp.setContentType("text/html;charset=utf-8");
                resp.getWriter().write("<script type=\"text/javascript\">\n" +
                        "\talert(\"签到成功\")\n" +
                        "\tsetTimeout(function () {\n" +
                        "\t\twindow.history.back(-1);\n" +
                        "    },100)\n" +
                        "</script>");
                    return;
                }
            if(vid.equals("0")&&status!=0){
                resp.setContentType("text/html;charset=utf-8");
                resp.getWriter().write("<script type=\"text/javascript\">\n" +
                        "\talert(\"已签到，不可重复签到\")\n" +
                        "\tsetTimeout(function () {\n" +
                        "\t\twindow.history.back(-1);\n" +
                        "    },100)\n" +
                        "</script>");
               return;
                }
            if(vid.equals("1")&&status==1){
                Calendar car1 = Calendar.getInstance();
                car1.setTime(date);
                Calendar car2 = Calendar.getInstance();
                car2.set(car1.get(Calendar.YEAR), car1.get(Calendar.MARCH), car1.get(Calendar.DAY_OF_MONTH), 18, 0, 0);
                int i = car2.compareTo(car1);
                String ss = i >= 0 ? "早退" : "正常";
                int aid=ints[1];
                attendanceService.updateAttend(aid,ss,s2,s3);
                resp.setContentType("text/html;charset=utf-8");
                resp.getWriter().write("<script type=\"text/javascript\">\n" +
                        "\talert(\"签退成功\")\n" +
                        "\tsetTimeout(function () {\n" +
                        "\t\twindow.history.back(-1);\n" +
                        "    },100)\n" +
                        "</script>");
                return;
            }
            if (vid.equals("1")&&status==0){
                resp.setContentType("text/html;charset=utf-8");
                resp.getWriter().write("<script type=\"text/javascript\">\n" +
                        "\talert(\"未签到,不可签退\")\n" +
                        "\tsetTimeout(function () {\n" +
                        "\t\twindow.history.back(-1);\n" +
                        "    },100)\n" +
                        "</script>");
            }else if(vid.equals("1")&&status==2){
                resp.setContentType("text/html;charset=utf-8");
                resp.getWriter().write("<script type=\"text/javascript\">\n" +
                        "\talert(\"已签退，不可重复签退\")\n" +
                        "\tsetTimeout(function () {\n" +
                        "\t\twindow.history.back(-1);\n" +
                        "    },100)\n" +
                        "</script>");
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
